var M = require("./config/index.js")

class ApiUtils {
    constructor(config) {
        this.Db=require("./db/index.js")(config);
     }
     /**
      * 执行postman生成的脚本
      * @param  r 
      */
     runSc(r) {
        r = r.replace(/var /g, "let ")
        r = r.replace(/console.log/g, "resolve")
        let sc = " new Promise((resolve, reject) => {"
            + r +
            " })";
    
        let rs = eval(sc)
        return rs;
    }

    async runApi(apiName) {
        try {
            M.apiName = apiName;
            let r = await this.Db.getApiSc(apiName)
            r = M.template(r);
            M.log(`start run apiName:${apiName} code:\n ${M.log_display_code ?r:""}`)
            r = await this.runSc(r);
            M.log(`${apiName} result \n ${r}`)
            try{
                return JSON.parse(r);
            }catch(e){
                return r;
            }
        } catch (e) {
            M.testCaseLog = M.testCaseLog + e;
            console.error(e)
        }
    }

    async runApiGroup (apiName) {
        try {
            M.apiName = apiName;
            let r = await this.Db.getApiSc(apiName)
            r = M.template(r);
            r = r.replace(/const \{M,ApiUtils\}.+\n/, "");
            r = r.replace("U=new ApiUtils();", "");
            M.log(`start run groupName:${apiName} code:\n  ${M.log_display_code ?r:""}`)
            let rs = eval(r)
            return rs;
        } catch (e) {
            M.testCaseLog = M.testCaseLog + e;
            console.error(e)
        }
    }

    expectEql(s, t) {
        if (s == t) {
            let o = {};
            o.apiName = M.apiName;
            o.success = true;
            o.message = "测试通过"
            M.testCaseResultList.push(o)
        } else {
            let o = {};
            o.apiName = M.apiName;
            o.success = false;
            o.message = "测试失败"
            o.reason = `${M.apiName}接口,期望值${s},实际值${t}`
            M.testCaseResultList.push(o)
        }
        if (M.runApiDelay) {
            M.sleep(M.runApiDelay);
        }
    
    }


    /**
     * 开始测试一个模块
     * @param {*} groupName 
     */
    startTest(groupName) {
        M.testCase[groupName] = [];
        M.testCaseResultList = M.testCase[groupName];
        M.testCaseLog = "";
    }

    /**
     * 测试一个模块结束
     * @param {*} groupName 
     */
    async endTest (groupName) {
        console.log("======", groupName + "测试完成")
        /**
         * 测试结果入库
         */
        let o = {};
        o.execute_log = M.testCaseLog;
        o.success_count = 0;
        o.total_count = M.testCase[groupName].length;
        for (let i = 0; i < M.testCase[groupName].length; i++) {
            if (M.testCase[groupName][i].success) {
                o.success_count=o.success_count+1;
            }
        }
        o.api_group_name = groupName;
        o.execute_result =JSON.stringify(M.testCase[groupName]);
        o.execute_log = M.testCaseLog.replace(/\'/g, "''");;
        o.time=new Date().format("yyyy-mm-dd hh:MM:ss")
        //console.log(o)
        let sql = M.getInsertObjSql("mi_result", o)
        //console.log(sql)
        let rs = await this.Db.doSql(sql);
        M.testCaseLog = "";
    }
}







module.exports = { ApiUtils,M }; 